<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/5
 * Time: 8:34
 */
namespace app\api\controller;
use think\Db;
use think\Request;
use app\api\model\Hotel;
use app\api\model\Food;
use app\api\model\GroupTour;
use app\common\model\GuideModel;

class Move extends Nocommon
{
    /*
     * 添加酒店入住
     */
    public function insertHotel()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            //查询改用户是否已经入住
           // $is_user = Db::name('user')->where('uid',$post['uid'])->find();
            //查询后台是否有信息入驻
            $admin = Db::name('hotel_admin')->where('h_admin_name',$post['send_tel'])->find();
            if($admin){
                $this->apiReturn('400','该账号已入住平台');
            }
             $is_user = Db::name('user')->where('uid',$post['uid'])->find();
                if($is_user['hotel_id'] !=0){
                    $this->apiReturn('400','该账号已入住平台');
                }
            $hotel = new Hotel();
            $post['app_time'] = time();
            $data = [
                'user_id'=>$post['uid'],
                'hotel_name'=>$post['hotel_name'],
                'hotel_tel'=>$post['hotel_tel'],
                'city'=>$post['city'],
                'address'=>$post['address'],
                'fs_lagal'=>$post['fs_lagal'],
                'business_license'=>$post['business_license'],
                'live_action'=>$post['live_action'],
                'send_tel'=>$post['send_tel'],
                'app_time'=>time(),
            ];
            $insert = $hotel->insert_hotel($data);
            if($insert){

                $address =  $post['city'].$post['address'];
    
                $date = [
                    'longitude' =>getDoc($address)[0],
                    'latitude'  =>getDoc($address)[1],
                ];
                Db::name('hotel')->where('hotel_id',$insert)->update($date);
               
                $this->apiReturn('200','添加成功');
                
            }else{
                $this->apiReturn('400','添加失败');
            }
        }
    }

    /**
     * 美食店铺入住
     */
    public function food()
    {
        try{
            if(Request::instance()->isPost()){
                $post = Request::instance()->post();
                //查询改用户是否已经入住
                $is_user = Db::name('user')->where('uid',$post['uid'])->find();
                if($is_user['food_store_id'] !=0){
                    $this->apiReturn('400','该账号已入住平台');
                }
                $post['app_time'] = time();
                $data = [
                    'uid'=>$post['uid'],
                    'food_store_name'=>$post['food_store_name'],
                    'food_store_tel'=>$post['food_store_tel'],
                    'city'=>$post['city'],
                    'food_store_address'=>$post['food_store_address'],
                    'fs_lagal'=>$post['fs_lagal'],
                    'business_license'=>$post['business_license'],
                    'live_action'=>$post['live_action'],
                    'food_store_tag'=>$post['food_store_tag'],
                    'send_tel'=>$post['send_tel'],
                    'app_time'=>time(),
                ];

                $insert = Db::name('food_store')->insertGetId($data);
                if($insert){
                    $address = $post['city'].$post['food_store_address'];


                    $date = [
                        'food_store_longitude' =>getDoc($address)[0],
                        'food_store_latitude'  =>getDoc($address)[1],

                    ];

                    Db::name('food_store')->where('food_store_id',$insert)->update($date);

                    $this->apiReturn('200','添加成功');


                }else{
                    $this->apiReturn('400','添加失败');
                }
            }
        } catch (\Exception $e) {


        }

    }

    /*
     * 旅行社入住
     */
    public function insertTeam()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            //查询改用户是否已经入住
            $admin = Db::name('team_admin')->where('t_admin_tel',$post['send_tel'])->find();
            if($admin){
                $this->apiReturn('400','该账号已入住平台');
            }
             $is_user = Db::name('user')->where('uid',$post['uid'])->find();
            if($is_user['team_id'] !=0){
                $this->apiReturn('400','该账号已入住平台');
            }
            $team = new GroupTour();
            $post['app_time'] = time();
            $post['commission_rate'] = 0;
            $data = [
                'uid'=>$post['uid'],
                'team_name'=>$post['team_name'],
                'team_phone'=>$post['team_phone'],
                'city'=>$post['city'],
                'team_address'=>$post['team_address'],
                'team_legel_image'=>$post['team_legel_image'],
                'team_license_image'=>$post['team_license_image'],
                'team_image'=>$post['team_image'],
                'send_tel'=>$post['send_tel'],
                'app_time'=>time(),
            ];
            $insert = $team->insert_team($data);
            if($insert){
                $address = $post['city'].$post['team_address'];
            
                $data = [
                    'longitude' =>getDoc($address)[0],
                    'latitude'  =>getDoc($address)[1],
                ];
                 Db::name('team')->where('team_id',$insert)->update($data);
            
                $this->apiReturn('200','添加成功');
                
            }else{
                $this->apiReturn('400','添加失败');
            }
        }
    }

    /**
     * 导游入住
     */
    public function insertGuide()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            //查询改用户是否已经入住
            $is_user = Db::name('user')->where('uid',$post['uid'])->find();
            if($is_user['status'] !=0){
                $this->apiReturn('400','该账号已入住平台');
            }
            $guide = new GuideModel();
            $insert = $guide->insert_guide($post);
            if($insert){
                $this->apiReturn('200','添加成功');
            }else{
                $this->apiReturn('400','添加失败');
            }
        }
    }
}